﻿using System;
using System.Text;

namespace Alex.Library.Web.Ajax
{
    /// <summary>
    /// 序列化类型
    /// </summary>
    public enum SerializeType
    {
        /// <summary>
        /// 按JSON格式序列化
        /// </summary>
        JSON,
        /// <summary>
        /// 按XML格式序列化
        /// </summary>
        XML,
        /// <summary>
        /// 按文本格式序列化
        /// </summary>
        Text
    }

    /// <summary>
    /// 序列化基类
    /// </summary>
    public abstract class SerializeHelper : ISerializeHelper
    {
        /// <summary>
        /// 序列化是否包含
        /// </summary>
        public abstract bool IncludeNull { get; set; }
        public abstract string Serialize<T>(T obj, Encoding encoding);
        public abstract string Serialize(object obj, Encoding encoding);
        public abstract T Deserialize<T>(string serializeString, Encoding encoding);
        public abstract object Deserialize(string serializeString, Type type, Encoding encoding);

        /// <summary>
        /// 使用UTF8字符编码将对象序列化为字符串
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public virtual string Serialize<T>(T obj)
        {
            return Serialize<T>(obj, Encoding.UTF8);
        }
        
        /// <summary>
        /// 使用UTF8字符编码将对象序列化为字符串
        /// </summary>
        /// <param name="obj">对象</param>        
        /// <returns></returns>
        public virtual string Serialize(object obj)
        {
            return Serialize(obj, Encoding.UTF8);
        }

        /// <summary>
        /// 使用UTF8字符编码将字符串反序列化为对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="serializeString">JSON字符串</param>
        /// <returns></returns>
        public virtual T Deserialize<T>(string serializeString)
        {
            return Deserialize<T>(serializeString, Encoding.UTF8);
        }

        /// <summary>
        /// 使用UTF8字符编码将字符串反序列化为对象
        /// </summary>
        /// <param name="serializeString">字符串</param>
        /// <param name="type">反序列化对象的类型</param>        
        /// <returns></returns>
        public virtual object Deserialize(string serializeString, Type type)
        {
            return Deserialize(serializeString, type, Encoding.UTF8);
        }

        /// <summary>
        /// 根据类型获取默认的序列化对象
        /// </summary>
        /// <param name="type">序列化类型</param>
        /// <returns></returns>
        public static ISerializeHelper Eval(SerializeType type)
        {
            switch (type)
            {
                case SerializeType.JSON:
                    return new JsonSerializeHelper();
                case SerializeType.XML:
                    return new XmlSerializeHelper();
                case SerializeType.Text:
                    return new TextSerializeHelper();
                default:
                    return null;
            }
        }
    }
}
